#!/usr/bin/make -f

# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=

override_dh_auto_configure:
	@echo "RULES.$@"
	cmake . \
		-DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINSTALL_DOCDIR=share/mysql/docs \
		-DINSTALL_DOCREADMEDIR=share/mysql \
		-DINSTALL_INCLUDEDIR=include/mysql \
		-DINSTALL_INFODIR=share/mysql/docs \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DINSTALL_MANDIR=share/man \
		-DINSTALL_MYSQLSHAREDIR=share/mysql \
		-DINSTALL_MYSQLTESTDIR=lib/mysql-test \
		-DINSTALL_PLUGINDIR=lib/mysql/plugin \
		-DINSTALL_SBINDIR=sbin \
		-DINSTALL_SCRIPTDIR=bin \
		-DINSTALL_SQLBENCHDIR=lib/mysql \
		-DINSTALL_SUPPORTFILESDIR=share/mysql \
		-DMYSQL_DATADIR=/var/lib/mysql \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DWITH_SSL=bundled \
		-DWITH_ZLIB=system \
		-DWITH_EXTRA_CHARSETS=all \
		-DWITH_INNODB_MEMCACHED=1 \
		-DCOMPILATION_COMMENT="MySQL Community Server (GPL)" \
		-DINSTALL_LAYOUT=DEB

	cat CMakeCache.txt
	touch $@

override_dh_auto_build:
	@echo "RULES.$@"
	$(MAKE) -j8 VERBOSE=1
	touch $@

override_dh_auto_test:
	@echo "RULES.$@"
	echo "No tests run because test 9: pfs_connect_attr is failing unreasonably"
	touch $@

override_dh_auto_install:

	@echo "RULES.$@"
	# complete install first
	$(MAKE) install DESTDIR=debian/tmp
	# remove all redundant files and links
	rm debian/tmp/usr/lib/*/*_r*
	rm debian/tmp/usr/lib/mysql-test/cmake_install.cmake
	rm debian/tmp/usr/lib/mysql-test/CTestTestfile.cmake
	rm debian/tmp/usr/lib/mysql-test/Makefile
	# add missing man pages
	install -g root -o root -m 0644 debian/extra/mysql_embedded.1 debian/tmp/usr/share/man/man1
	# add MySQL Server configuration file my.cnf to mysql-common package
	install -g root -o root -m 0644 -D debian/extra/my.cnf debian/tmp/etc/mysql/my.cnf
	# add MySQL Server debug binary and library to package
	install -g root -o root -m 0755 debian/extra/server-binary debian/tmp/usr/sbin/mysqld-debug
	install -g root -o root -m 0755 debian/extra/embedded-server debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libmysqld-debug.a
	# add debug plugin libraries to package
	install -g root -o root -m 0755 -d debian/tmp/usr/lib/mysql/plugin/debug
	for file in debian/extra/*-plugin; do NEW=`echo $$file | cut -d- -f1`; mv $$file $$NEW.so; done
	install -g root -o root -m 0755 debian/extra/*.so debian/tmp/usr/lib/mysql/plugin/debug
	install -g root -o root -m 0755 debian/extra/daemon_example.ini debian/tmp/usr/lib/mysql/plugin/debug
	# add apparmor profile
	install -g root -o root -m 0644 -D debian/extra/apparmor-profile debian/tmp/etc/apparmor.d/usr.sbin.mysqld
	# add directory for legal docs
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-server
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-server
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-client
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-client
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-common
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/libmysqlclient18
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/libmysqlclient-dev
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/libmysqld-dev
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-bench
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-testsuite
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-test
	install -g root -o root -m 0755 -d debian/tmp/usr/share/doc/mysql-community-source
	# add COPYING file to each package
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-server/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-server/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-client/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-client/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-common/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/libmysqlclient18/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/libmysqlclient-dev/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/libmysqld-dev/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-bench/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-testsuite/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-test/COPYING
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/COPYING debian/tmp/usr/share/doc/mysql-community-source/COPYING
	# add README file to each package
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-server/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-server/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-client/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-client/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-common/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/libmysqlclient18/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/libmysqlclient-dev/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/libmysqld-dev/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-bench/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-testsuite/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-test/README
	install -g root -o root -m 0644 debian/tmp/usr/share/mysql/README debian/tmp/usr/share/doc/mysql-community-source/README
	touch $@

override_dh_installinit:
	@echo "RULES.$@"
	dh_apparmor -pmysql-community-server --profile-name=usr.sbin.mysqld
	dh_installinit --name=mysql -- defaults 19 21
	touch $@
